<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item> <i class="el-icon-lx-cascades"></i> 公司红包账户 </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button @click="exportExcel" type="primary" class="button" style="margin-right: 10px">导出数据</el-button>
                <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
                    <#list classInfo.fieldList as fieldItem >
                        <el-input v-model="query.${fieldItem.fieldName}" placeholder="${fieldItem.fieldComment}" class="handle-input mr10"></el-input>

                    </#list>
                </#if>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>

            <el-table :data="tableDataAll" style="display: none" id="alldata">
                <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
                    <#list classInfo.fieldList as fieldItem >
                        <el-table-column prop="${fieldItem.fieldName}" label="${fieldItem.fieldComment}"
                                         align="center"></el-table-column>
                    </#list>
                </#if>

            </el-table>

            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
                    <#list classInfo.fieldList as fieldItem >
                        <el-table-column prop="${fieldItem.fieldName}" label="${fieldItem.fieldComment}"
                                         align="center"></el-table-column>
                    </#list>
                </#if>
                <el-table-column label="操作"  align="center">
<#--                    v-if="scope.row.deleteFlag === 0"-->
                    <template slot-scope="scope" >
                        <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="load"
                >
                </el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :rules="formRules" :model="form" label-width="70px">

                <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
                    <#list classInfo.fieldList as fieldItem >

                        <el-form-item label="${fieldItem.fieldComment}" prop="${fieldItem.fieldName}">
                            <el-input v-model="form.${fieldItem.fieldName}"></el-input>
                        </el-form-item>
                    </#list>
                </#if>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button
                        @click="
                        editVisible = false;
                        getData();
                    "
                >取 消</el-button
                >
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import FileSaver from 'file-saver';
    import XLSX from 'xlsx';
    export default {
        name: '${classInfo.className}Table',
        data() {
            return {
                tableName:"${classInfo.className?uncap_first}",
                radio: false,
                value: '选项1',
                query: {
            <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
            <#list classInfo.fieldList as fieldItem >
            ${fieldItem.fieldName}:null,
            </#list>
            </#if>
                pageIndex: 1,
                pageSize: 10
                },
                tableData: [],
                tableDataAll: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                pageTotal: 0,
                form: {},

                editVisible2: false,
                formadmin: {},
                passwordnew: '',
                password: '',
                idx: -1,
                id: -1,
                formRules: {
                    companyRest: [{ required: true, message: '请输入红包账户余额', trigger: 'blur' }]
                }
            };
        },
        created() {
            this.$axios
                .get(this._api + 'admin/token', {
                    params: {},
                    headers: {
                        token: this.$cookies.get('token'),
                        'Content-Type': 'application/json;charset=utf-8' //头部信息
                    }
                })
                .then((response) => {
                if (response.data.port == '200') {
                if (response.data.role != 'Superadmin') {
                    this.$message.error('权限不足');
                    this.$router.go(-1);
                }

                this.getData();
            } else {
                this.$message.error(response.data.msg);
                this.$router.go(-1);
            }
        })
        .catch(function (error) {
                console.log(error);
            });
        },
        methods: {
            formatDate(row, column) {
                // 获取单元格数据
                let data = row[column.property];
                let dt = new Date(data);
                return (
                    dt.getFullYear() +
                    '-' +
                    (dt.getMonth() + 1 > 9 ? dt.getMonth() + 1 : '0' + (dt.getMonth() + 1)) +
                    '-' +
                    (dt.getDate() > 9 ? dt.getDate() : '0' + dt.getDate()) +
                    ' ' +
                    (dt.getHours() > 9 ? dt.getHours() : '0' + dt.getHours()) +
                    ':' +
                    (dt.getMinutes() > 9 ? dt.getMinutes() : '0' + dt.getMinutes()) +
                    ':' +
                    (dt.getSeconds() > 9 ? dt.getSeconds() : '0' + dt.getSeconds())
                );
            },

            exportExcel() {
                let time = new Date();
                let year = time.getFullYear();
                let month = time.getMonth() + 1;
                let day = time.getDate();
                let name = year + '' + month + '' + day + '-公司红包账户';
                var xlsxParam = { raw: true };
                var wb = XLSX.utils.table_to_book(document.querySelector('#alldata'), xlsxParam);
                var wbout = XLSX.write(wb, {
                    bookType: 'xlsx',
                    bookSST: true,
                    type: 'array'
                });
                try {
                    FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), name + '.xlsx');
                } catch (e) {
                    if (typeof console !== 'undefined') console.log(e, wbout);
                }
                return wbout;
            },

            // 获取数据
            getData() {
                if (this.radio) {
                    this.query.flag = -1;
                } else {
                    this.query.flag = 0;
                }
                let params = {

                    pageIndex: this.query.pageIndex - 1,
                    pageSize: this.query.pageSize,
                <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
                <#list classInfo.fieldList as fieldItem >
                ${fieldItem.fieldName}:this.query.${fieldItem.fieldName},
                </#list>
                </#if>
                };
                // https://blog.csdn.net/niuch1029291561/article/details/8190617
                this.$axios
                    .post(this._api + '${classInfo.className?uncap_first}/list', params, {
                        headers: {
                            token: this.$cookies.get('token'),
                            'Content-Type': 'application/json;charset=utf-8' //头部信息
                        }
                    })
                    .then((response) => {
                    this.tableData = [];
                // this.tableData = response.data.data.content;
                // this.pageTotal = response.data.data.totalElements;

                this.tableData =  response.data.content||response.data.data.content
                this.pageTotal =response.data.totalElements|| response.data.data.totalElements

                console.log(this.tableData);
            })
            .catch(function (error) {
                    console.log(error);
                });
                let data={
                <#if classInfo.fieldList?exists && classInfo.fieldList?size gt 0>
                <#list classInfo.fieldList as fieldItem >
                ${fieldItem.fieldName}:null,
                </#list>
                </#if>
                    pageIndex: 0,
                    pageSize: 100000,
                }
                this.$axios
                    .post(
                        this._api + '${classInfo.className?uncap_first}/list', data,
                        {
                            headers: {
                                token: this.$cookies.get('token'),
                                'Content-Type': 'application/json;charset=utf-8' //头部信息
                            }
                        }
                    )
                    .then((response) => {
                    this.tableDataAll = [];
                this.tableDataAll = response.data.content||response.data.data.content;
            })
            .catch(function (error) {
                    console.log(error);
                });
            },
            // 触发搜索按钮
            handleSearch() {
                this.$set(this.query, 'pageIndex', 1);
                this.getData();
            },

            // 编辑操作
            handleEdit(index, row) {
                if (row.deleteFlag == 1) {
                    this.$message.error('该单位不可用！');
                } else {
                    this.idx = index;
                    this.form = row;
                    this.editVisible = true;
                }
            },

            // 保存编辑
            saveEdit() {
                if (this.form.companyRest < 0) {
                    this.$message.error('账户余额不能小于0');
                    this.getData();
                    return false;
                }
                if (this.form.companyRest == '') {
                    this.$message.error('账户余额不能为空');
                    this.getData();
                    return false;
                }

                let params = this.form;
                this.$axios
                    .post(this._api + '${classInfo.className?uncap_first}/save', params, {
                        headers: {
                            token: this.$cookies.get('token'),
                            'Content-Type': 'application/json;charset=utf-8'
                        } //头部信息
                    })
                    .then((response) => {
                    if (response.data.port == '200') {
                    this.$message.success('修改成功！');
                    this.editVisible = false;
                    this.getData();
                } else {
                    this.$message.error(response.data.msg);
                }
            })
            .catch(function (error) {
                    console.log(error);
                });
            },

            // 分页导航
            load(val) {
                this.$set(this.query, 'pageIndex', val);
                this.getData();
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 200px;
        display: inline-block;
    }
    .table {
        width: 100%;
        font-size: 14px;
    }
    .red {
        color: #ff0000;
    }
    .green {
        color: green;
    }
    .grey {
        color: grey;
    }
    .mr10 {
        margin-right: 10px;
    }
    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
